package com.circle.model.util;

import com.circle.model.entity.DccInternalFileEntity;
import com.circle.model.entity.DccOutFileEntity;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DataConverter {
    /**
     * 将List<Map<String, Object>>转换为二维数组
     * @param dataList 数据列表
     * @param columnNames 列名数组，用于确定数据顺序
     * @return 二维数组
     */
    public static Object[][] toArray(List<Map<String, Object>> dataList, String[] columnNames) {
        if (dataList == null || dataList.isEmpty() || columnNames == null || columnNames.length == 0) {
            return new Object[0][0];
        }

        Object[][] array = new Object[dataList.size()][columnNames.length];
        
        for (int i = 0; i < array.length; i++) {
            Map<String, Object> row = dataList.get(i);
            for (int j = 0; j < columnNames.length; j++) {
                array[i][j] = row.get(columnNames[j]);
            }
        }
        
        return array;
    }

    /**
     * 将单个Map转换为一维数组
     * @param data Map数据
     * @param columnNames 列名数组，用于确定数据顺序
     * @return 一维数组
     */
    public static Object[] toArray(Map<String, Object> data, String[] columnNames) {
        if (data == null || columnNames == null || columnNames.length == 0) {
            return new Object[0];
        }

        Object[] array = new Object[columnNames.length];
        for (int i = 0; i < columnNames.length; i++) {
            array[i] = data.get(columnNames[i]);
        }
        
        return array;
    }

    public static String[] toArraySingleField(List<Map<String, Object>> dataList, String fieldName){
        if (CheckUtils.checkIsNull(dataList)){
            return new String[0];
        }
        String[] array = new String[dataList.size()];
        for (int i = 0; i < array.length; i++) {
            array[i] = dataList.get(i).get(fieldName).toString();
        }
        return array;
    }

    public static List<DccInternalFileEntity> convertToEntityList(List<Map<String, Object>> results) {
        return results.stream()
                .map(map -> {
                    DccInternalFileEntity entity = new DccInternalFileEntity();
                    entity.setId((String) map.get("id"));
                    entity.setTitle((String) map.get("title"));
                    entity.setSpecNo((String) map.get("spec_no"));
                    entity.setRev((String) map.get("rev"));
                    entity.setOwnerName((String) map.get("owner_name"));
                    entity.setOwnerNo((String) map.get("owner_no"));
                    entity.setDeptName((String) map.get("dept_name"));
                    entity.setDocType((String) map.get("doc_type"));
                    return entity;
                })
                .collect(Collectors.toList());
    }

    public static List<DccOutFileEntity> convertToEntityForOutList(List<Map<String, Object>> results) {
        return results.stream()
                .map(map -> {
                    DccOutFileEntity entity = new DccOutFileEntity();
                    entity.setId((String) map.get("id"));
                    entity.setTitle((String) map.get("title"));
                    entity.setSpecNo((String) map.get("spec_no"));
                    entity.setRev((String) map.get("rev"));
                    entity.setOwnerName((String) map.get("owner_name"));
                    entity.setOwnerNo((String) map.get("owner_no"));
                    entity.setDeptName((String) map.get("dept_name"));
                    entity.setDocType((String) map.get("doc_type"));
                    return entity;
                })
                .collect(Collectors.toList());
    }
} 